//
//  DCAlipayProxy.m
//  LibWeexDCAlipay
//
//  Created by yanyu on 2019/5/14.
//  Copyright © 2019 yanyu. All rights reserved.
//

#import "DCAlipayProxy.h"
#import <AFAuthSDK/AFAuthSDK.h>

@implementation DCAlipayProxy
-(void)onCreateUniPlugin{
    NSLog(@"DCAlipaySSOProxy 有需要初始化的逻辑可以放这里！");
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
    NSLog(@"DCAlipaySSOProxy 有需要didFinishLaunchingWithOptions可以放这里！");
    return YES;
}


- (BOOL)application:(UIApplication *_Nullable)application handleOpenURL:(NSURL *_Nullable)url{
    if ([url.host isEqualToString:@"safepay"]) {
        //授权返回码
        [[AFAuthSDK defaultService]processAuthv2Result:url standbyCallback:^(NSDictionary *result) {
            // 解析 auth code
            NSString *resultString = result[@"result"];
            NSString *authCode = nil;
            if (resultString.length>0) {
                NSArray *resultArr = [resultString componentsSeparatedByString:@"&"];
                for (NSString *subResult in resultArr) {
                    if (subResult.length > 10 && [subResult hasPrefix:@"auth_code="]) {
                        authCode = [subResult substringFromIndex:10];
                        break;
                    }
                }
            }
            [[NSNotificationCenter defaultCenter] postNotificationName:@"AliplySSO" object:result];
        }];
    }
    
    return TRUE;
}
@end
